package com.news.news.service;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.news.news.dao.UserCenterDao;
import com.news.news.modle.News;
import com.news.news.modle.User;
import com.news.news.util.JWTUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserNewsService {

    @Resource
    UserCenterDao userCenterDao;


    public List<News> mynewsList(String token) {
        Integer id = JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        return userCenterDao.mynewsList(id);
    }

    public Integer findUser(String password) {
        User user = userCenterDao.findUser(password);
        if(user==null){
            return 0;
        }
        return 1;
    }

    public void deleteNews(int id) {
        userCenterDao.deleteNews(id);
    }

    public News findNewsById(int id) {
        News newsById = userCenterDao.findNewsById(id);
        if(newsById.getNewsTypeList()!=null){
            //将新闻类型的列表取出id,转化为数组
            Integer[] newsTypeid = new Integer[newsById.getNewsTypeList().size()];
            for (int i = 0; i <newsTypeid.length ; i++) {
                newsTypeid[i]=newsById.getNewsTypeList().get(i).getId();
            }
            //存入数组
            newsById.setNewsTypeid(newsTypeid);
        }
        return newsById;
    }
}
